/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;

import Modelo.Jugador;
import Modelo.ModPartida;
import Vista.VistaPart;
import bean.Music;
import java.awt.Color;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import bean.Reloj;

/**
 *
 * @author USUARIO
 */
public class ContPartida {

    private VistaPart VP;
    private ModPartida M;
    private Vector<JLabel> labeles = new Vector<JLabel>();
    private Vector Palabras = new Vector();
    private Reloj R = new Reloj();
    Music Reproducion = new Music();
    private int Nivel = 0;

    ContPartida(VistaPart VP, ModPartida M, int Nivel) throws SQLException, Exception {
        this.Reproducion.AbrirFichero("C:/Documents and Settings/Robert Alvarado/Escritorio/FINAL_COLOR/1-1-primer-proyecto/src/Musica/1.mp3");
        this.Reproducion.Play();
        this.VP = VP;
        this.M = M;
        this.VP.setVisible(true);
        this.Nivel = Nivel + 1;
        this.agregarVectLabel();
        R.actualizacion(this.VP.getReloj());
        this.IniciarPartida();

    }

    public void IniciarPartida() throws SQLException {
        R.Parar();
        R.Reiniciar(this.VP.getReloj());
        R.Iniciar();
        this.VP.HablitarRegistro(false);
        this.M.setError(0);
        this.M.setAcierto(0);
        this.Imagenes();
        
        if(this.Nivel == 1){
            this.M.BuscarPalabra();
            this.LimpiarLabeles();
        }else if(this.Nivel == 2){
            this.M.BuscarPalabra2();
            this.LimpiarLabeles2();
            this.LimpiarVacios(" ", " ");
        }else{
            this.M.BuscarPalabra3();
            this.LimpiarLabeles2();
            this.LimpiarVacios(" ", " ");
        }
        
        this.HabilitarTeclado(true);
        this.VP.setMensaje("", false, Color.RED);
    }

    public Reloj getR() {
        return R;
    }

    public void ContarError() {
        this.M.ContarError();
        this.Imagenes();
    }

    public void Salir() throws Exception{
        this.Reproducion.Stop();
        this.VP.dispose();
    }
    
    public void Imagenes() {

        switch (this.M.getError()) {
            case 0:
                this.VP.setAhorcado("Imagenes/Aho00.gif");
                break;
            case 1:
                this.VP.setAhorcado("Imagenes/Aho01.gif");
                break;
            case 2:
                this.VP.setAhorcado("Imagenes/Aho0.gif");
                break;
            case 3:
                this.VP.setAhorcado("Imagenes/Aho2.gif");
                break;
            case 4:
                this.VP.setAhorcado("Imagenes/Aho3.gif");
                break;
            case 5:
                this.VP.setAhorcado("Imagenes/Aho4.gif");
                break;
            case 6:
                this.VP.setAhorcado("Imagenes/Aho5.gif");
                break;
            case 7:
                this.VP.setAhorcado("Imagenes/Aho6.gif");
                break;
            case 8:
                this.VP.setAhorcado("Imagenes/Aho7.gif");
                break;

        }

    }

    public void agregarVectLabel() {
        this.labeles.add(this.VP.getjLabel1());
        this.labeles.add(this.VP.getjLabel2());
        this.labeles.add(this.VP.getjLabel3());
        this.labeles.add(this.VP.getjLabel4());
        this.labeles.add(this.VP.getjLabel5());
        this.labeles.add(this.VP.getjLabel6());
        this.labeles.add(this.VP.getjLabel7());
        this.labeles.add(this.VP.getjLabel8());
        this.labeles.add(this.VP.getjLabel9());
        this.labeles.add(this.VP.getjLabel10());
        this.labeles.add(this.VP.getjLabel11());
        this.labeles.add(this.VP.getjLabel12());
        this.labeles.add(this.VP.getjLabel13());
        this.labeles.add(this.VP.getjLabel14());
        this.labeles.add(this.VP.getjLabel15());
        this.labeles.add(this.VP.getjLabel16());
        this.labeles.add(this.VP.getjLabel17());
        this.labeles.add(this.VP.getjLabel18());
        this.labeles.add(this.VP.getjLabel19());
        this.labeles.add(this.VP.getjLabel20());
        this.labeles.add(this.VP.getjLabel21());
        this.labeles.add(this.VP.getjLabel22());
        this.labeles.add(this.VP.getjLabel23());
        this.labeles.add(this.VP.getjLabel24());
        this.labeles.add(this.VP.getjLabel25());
        this.labeles.add(this.VP.getjLabel26());
        this.labeles.add(this.VP.getjLabel27());
        this.labeles.add(this.VP.getjLabel28());
        this.labeles.add(this.VP.getjLabel29());
        this.labeles.add(this.VP.getjLabel30());
        this.labeles.add(this.VP.getjLabel31());
        this.labeles.add(this.VP.getjLabel32());
        this.labeles.add(this.VP.getjLabel33());
        this.labeles.add(this.VP.getjLabel34());
        this.labeles.add(this.VP.getjLabel35());
        this.labeles.add(this.VP.getjLabel36());
        this.labeles.add(this.VP.getjLabel37());
        this.labeles.add(this.VP.getjLabel38());
        this.labeles.add(this.VP.getjLabel39());
        this.labeles.add(this.VP.getjLabel40());
        this.labeles.add(this.VP.getjLabel41());
        this.labeles.add(this.VP.getjLabel42());
        this.labeles.add(this.VP.getjLabel43());
        this.labeles.add(this.VP.getjLabel44());
        this.labeles.add(this.VP.getjLabel45());
        this.labeles.add(this.VP.getjLabel46());
        this.labeles.add(this.VP.getjLabel47());
        this.labeles.add(this.VP.getjLabel48());
        this.labeles.add(this.VP.getjLabel49());
        this.labeles.add(this.VP.getjLabel50());
        this.labeles.add(this.VP.getjLabel51());
        this.labeles.add(this.VP.getjLabel52());
        this.labeles.add(this.VP.getjLabel53());
        this.labeles.add(this.VP.getjLabel54());
        this.labeles.add(this.VP.getjLabel55());
        this.labeles.add(this.VP.getjLabel56());
        this.labeles.add(this.VP.getjLabel57());
        this.labeles.add(this.VP.getjLabel58());
        this.labeles.add(this.VP.getjLabel59());
        this.labeles.add(this.VP.getjLabel60());
        this.labeles.add(this.VP.getjLabel61());
        this.labeles.add(this.VP.getjLabel62());
        this.labeles.add(this.VP.getjLabel63());
        this.labeles.add(this.VP.getjLabel64());
        this.labeles.add(this.VP.getjLabel65());
        this.labeles.add(this.VP.getjLabel66());
        this.labeles.add(this.VP.getjLabel67());
        this.labeles.add(this.VP.getjLabel68());
        this.labeles.add(this.VP.getjLabel69());
        this.labeles.add(this.VP.getjLabel70());
        this.labeles.add(this.VP.getjLabel71());
        this.labeles.add(this.VP.getjLabel72());
        this.labeles.add(this.VP.getjLabel73());
        this.labeles.add(this.VP.getjLabel74());
        this.labeles.add(this.VP.getjLabel75());
        this.labeles.add(this.VP.getjLabel76());
        this.labeles.add(this.VP.getjLabel77());
        this.labeles.add(this.VP.getjLabel78());
        this.labeles.add(this.VP.getjLabel79());
        this.labeles.add(this.VP.getjLabel80());
        this.labeles.add(this.VP.getjLabel81());
        this.labeles.add(this.VP.getjLabel82());
        this.labeles.add(this.VP.getjLabel83());
        this.labeles.add(this.VP.getjLabel84());
        this.labeles.add(this.VP.getjLabel85());
        this.labeles.add(this.VP.getjLabel86());
        this.labeles.add(this.VP.getjLabel87());
        this.labeles.add(this.VP.getjLabel88());
        this.labeles.add(this.VP.getjLabel89());
        this.labeles.add(this.VP.getjLabel90());
        this.labeles.add(this.VP.getjLabel91());
        this.labeles.add(this.VP.getjLabel92());
        this.labeles.add(this.VP.getjLabel93());
        this.labeles.add(this.VP.getjLabel94());
        this.labeles.add(this.VP.getjLabel95());
        this.labeles.add(this.VP.getjLabel96());
        this.labeles.add(this.VP.getjLabel97());
        this.labeles.add(this.VP.getjLabel98());
        this.labeles.add(this.VP.getjLabel99());
        this.labeles.add(this.VP.getjLabel100());
        this.labeles.add(this.VP.getjLabel101());
        this.labeles.add(this.VP.getjLabel102());
        this.labeles.add(this.VP.getjLabel103());
        this.labeles.add(this.VP.getjLabel104());
        this.labeles.add(this.VP.getjLabel105());
        this.labeles.add(this.VP.getjLabel106());
        this.labeles.add(this.VP.getjLabel107());
        this.labeles.add(this.VP.getjLabel108());
        this.labeles.add(this.VP.getjLabel109());
        this.labeles.add(this.VP.getjLabel110());
        this.labeles.add(this.VP.getjLabel111());
        this.labeles.add(this.VP.getjLabel112());
        this.labeles.add(this.VP.getjLabel113());
        this.labeles.add(this.VP.getjLabel114());
        this.labeles.add(this.VP.getjLabel115());
        this.labeles.add(this.VP.getjLabel116());
        this.labeles.add(this.VP.getjLabel117());
        this.labeles.add(this.VP.getjLabel118());
        this.labeles.add(this.VP.getjLabel119());
        this.labeles.add(this.VP.getjLabel120());
        this.labeles.add(this.VP.getjLabel121());
        this.labeles.add(this.VP.getjLabel122());
        this.labeles.add(this.VP.getjLabel123());
    }

    public void LimpiarLabeles() {

        for (int a = 0; a < this.labeles.size(); a++) {
            this.labeles.elementAt(a).setVisible(true);
        }
        for (int a = 0; a < this.M.getPalabraOri().size(); a++) {
            this.labeles.elementAt(a).setText("_");
        }

        for (int a = (this.M.getPalabraOri().size()); a <= 122; a++) {
            this.labeles.elementAt(a).setVisible(false);
        }
    }

    public void LimpiarLabeles2() {

        int P = 0;
        for (int a = 0; a < this.M.getPalabraOri2().size(); a++) {
            if (this.M.getPalabraOri2().elementAt(a) == " ") {
                P++;
            }
        }
        P++;     

        this.Palabras.clear();

        String Frase = "";
        for (int a = 0; a < this.M.getPalabraOri2().size(); a++) {
            if (this.M.getPalabraOri2().elementAt(a).toString().charAt(0) == ' ') {
                this.Palabras.add(Frase);
                Frase = "";
            } else if (a == this.M.getPalabraOri2().size() - 1) {
                Frase = Frase + this.M.getPalabraOri2().elementAt(a);
                this.Palabras.add(Frase);
                Frase = "";
            } else {
                Frase = Frase + this.M.getPalabraOri2().elementAt(a);
            }
        }



        for (int a = 0; a < this.labeles.size(); a++) {
            this.labeles.elementAt(a).setVisible(true);
        }

        this.M.getPalabraOri2().clear();
        String Frase2 = "";

        int Fila1 = 33;
        int Fila2 = 30;
        int Fila3 = 30;
        int Fila4 = 30;

        boolean R1 = false;
        boolean R2 = false;
        boolean R3 = false;
        boolean R4 = false;

        for (int u = 0; u < this.Palabras.size(); u++) {
            Frase2 = "";
            Frase2 = this.Palabras.elementAt(u).toString();

            if (Frase2.length() <= Fila1) {
                Fila1 = Fila1 - Frase2.length() - 1;
                for (int g = 0; g < Frase2.length(); g++) {
                    this.M.getPalabraOri2().add(Frase2.charAt(g));
                }
                this.M.getPalabraOri2().add(" ");
            } else if (R1 == false) {
                for (int y = 0; y < Fila1; y++) {
                    this.M.getPalabraOri2().add(" ");
                    this.labeles.elementAt(32 - y).setVisible(false);
                }

                if (Frase2.length() <= Fila2) {
                    Fila2 = Fila2 - Frase2.length() - 1;
                    for (int g = 0; g < Frase2.length(); g++) {
                        this.M.getPalabraOri2().add(Frase2.charAt(g));
                    }
                    this.M.getPalabraOri2().add(" ");
                }
                R1 = true;



            } else if (Frase2.length() <= Fila2) {
                Fila2 = Fila2 - Frase2.length() - 1;
                for (int g = 0; g < Frase2.length(); g++) {
                    this.M.getPalabraOri2().add(Frase2.charAt(g));
                }
                this.M.getPalabraOri2().add(" ");
            } else if (R2 == false) {
                for (int y = 0; y < Fila2; y++) {
                    this.M.getPalabraOri2().add(" ");
                    this.labeles.elementAt(62 - y).setVisible(false);
                }
                if (Frase2.length() <= Fila3) {
                    Fila3 = Fila3 - Frase2.length() - 1;
                    for (int g = 0; g < Frase2.length(); g++) {
                        this.M.getPalabraOri2().add(Frase2.charAt(g));
                    }
                    this.M.getPalabraOri2().add(" ");
                }
                R2 = true;

            } else if (Frase2.length() <= Fila3) {
                Fila3 = Fila3 - Frase2.length() - 1;
                for (int g = 0; g < Frase2.length(); g++) {
                    this.M.getPalabraOri2().add(Frase2.charAt(g));
                }
                this.M.getPalabraOri2().add(" ");
            } else if (R3 == false) {
                for (int y = 0; y < Fila3; y++) {
                    this.M.getPalabraOri2().add(" ");
                    this.labeles.elementAt(92 - y).setVisible(false);
                }

                if (Frase2.length() <= Fila4) {
                    Fila4 = Fila4 - Frase2.length() - 1;
                    for (int g = 0; g < Frase2.length(); g++) {
                        this.M.getPalabraOri2().add(Frase2.charAt(g));
                    }
                    this.M.getPalabraOri2().add(" ");
                    R3 = true;
                }
            } else if (Frase2.length() <= Fila4) {
                Fila4 = Fila4 - Frase2.length() - 1;
                for (int g = 0; g < Frase2.length(); g++) {
                    this.M.getPalabraOri2().add(Frase2.charAt(g));
                }
                this.M.getPalabraOri2().add(" ");
            } else if (R4 == false) {
                for (int y = 0; y < Fila4; y++) {
                    this.M.getPalabraOri2().add(" ");
                    this.labeles.elementAt(122 - y).setVisible(false);
                }
                R4 = true;
            }
        }
        
 

        for (int a = 0; a < this.M.getPalabraOri2().size(); a++) {
            this.labeles.elementAt(a).setText("_");

        }

        for (int a = (this.M.getPalabraOri2().size()); a <= 122; a++) {
            this.labeles.elementAt(a).setVisible(false);
        }
    }


    

    public void PonerLetra(char L, String L2,char L3){
        if(this.Nivel == 1){
            this.PonerLetra(L, L2);
        }else{
            this.PonerLetra2(L3, L2);
        }
    }
    
    public void PonerLetra(char L, String L2) {

        if (this.M.BuscarLetra(L)) {
            for (int a = 0; a < this.M.getPalabraOri().size(); a++) {

                for (int b = 0; b < this.M.getPosicion().size(); b++) {
                    if ((a + 1) == this.M.getPosicion().elementAt(b)) {
                        this.labeles.elementAt(a).setText(L2);
                    }
                }
            }
            this.M.LimpiarPosicion();
            if (this.M.Gano()) {
                this.VP.setMensaje("GANASTE", true, Color.GREEN);
                this.HabilitarTeclado(false);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(true);
                this.R.Parar();
            }
        } else {
            this.ContarError();
            if (this.M.SeguirJugando() == false) {
                this.HabilitarTeclado(false);
                this.VP.setMensaje("PERDISTE", true, Color.RED);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(false);
                this.R.Parar();
                this.R.Reiniciar(this.VP.getReloj());
            }

        }

    }

    public void PonerLetra2(char L, String L2) {

        if (this.M.BuscarLetra2(L)) {
            for (int a = 0; a < this.M.getPalabraOri2().size(); a++) {

                for (int b = 0; b < this.M.getPosicion().size(); b++) {
                    if ((a + 1) == this.M.getPosicion().elementAt(b)) {
                        this.labeles.elementAt(a).setText(L2);
                    }
                }
            }
            this.M.LimpiarPosicion();
            if (this.M.Gano2()) {
                this.VP.setMensaje("GANASTE", true, Color.GREEN);
                this.HabilitarTeclado(false);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(true);
                this.R.Parar();
            }
        } else {
            this.ContarError();
            if (this.M.SeguirJugando() == false) {
                this.HabilitarTeclado(false);
                this.VP.setMensaje("PERDISTE", true, Color.RED);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(false);
                this.R.Parar();
                this.R.Reiniciar(this.VP.getReloj());
            }

        }

    }
    
    public void LimpiarVacios(String L, String L2) {

        if (this.M.BuscarLetraV(L)) {
            for (int a = 0; a < this.M.getPalabraOri2().size(); a++) {

                for (int b = 0; b < this.M.getPosicion().size(); b++) {
                    if ((a + 1) == this.M.getPosicion().elementAt(b)) {
                        this.labeles.elementAt(a).setText(L2);
                    }
                }
            }
            this.M.LimpiarPosicion();
            if (this.M.Gano2()) {
                this.VP.setMensaje("GANASTE", true, Color.GREEN);
                this.HabilitarTeclado(false);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(true);
                this.R.Parar();
            }
        } else {
            this.ContarError();
            if (this.M.SeguirJugando() == false) {
                this.HabilitarTeclado(false);
                this.VP.setMensaje("PERDISTE", true, Color.RED);
                this.M.setAcierto(0);
                this.VP.HablitarRegistro(false);
                this.R.Parar();
                this.R.Reiniciar(this.VP.getReloj());
            }

        }

    }
    

    public void GuardarPuntaje() {
        
        if (!this.VP.getJNombre().getText().isEmpty()) {
            Jugador J = new Jugador(this.VP.getJNombre().getText(), this.Nivel, this.VP.getReloj().getText());
            this.R.Reiniciar(this.VP.getReloj());
            this.VP.HablitarRegistro(false);
        }else{
            this.VP.MostrarMensaje();
        }
    }

    public void HabilitarTeclado(boolean a) {

        this.VP.setjButton1(a);
        this.VP.setjButton2(a);
        this.VP.setjButton3(a);
        this.VP.setjButton4(a);
        this.VP.setjButton5(a);
        this.VP.setjButton6(a);
        this.VP.setjButton7(a);
        this.VP.setjButton8(a);
        this.VP.setjButton9(a);
        this.VP.setjButton10(a);
        this.VP.setjButton11(a);
        this.VP.setjButton12(a);
        this.VP.setjButton13(a);
        this.VP.setjButton14(a);
        this.VP.setjButton15(a);
        this.VP.setjButton16(a);
        this.VP.setjButton17(a);
        this.VP.setjButton18(a);
        this.VP.setjButton19(a);
        this.VP.setjButton20(a);
        this.VP.setjButton21(a);
        this.VP.setjButton22(a);
        this.VP.setjButton23(a);
        this.VP.setjButton24(a);
        this.VP.setjButton25(a);
        this.VP.setjButton26(a);
        this.VP.setjButton27(a);

    }
}
